package com.simon.app.october.common.exception;

import com.simon.app.october.common.utils.Result;
import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class OctoberExceptionHandler {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        logger.error(e.getMessage(), e);
        return Result.error();
    }

    @ExceptionHandler(OctoberException.class)
    public Result handleOctoberException(OctoberException e) {
        logger.error(e.getMessage(), e);
        return Result.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public Result handleDuplicateKeyException(DuplicateKeyException e) {
        logger.error(e.getMessage(), e);
        return Result.error(501,"已经存在该记录了！");
    }

    @ExceptionHandler(AuthorizationException.class)
    public Result handleAuth(AuthorizationException e) {
        logger.error(e.getMessage(), e);
        return Result.error(502, "您没有权限操作");
    }
}
